<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\repair\service;

use app\repair\model\RepairPostModel;

class PostService
{

    public function adminArticleList($filter)
    {
        return $this->adminPostList($filter);
    }

    //查询单页面
    public function adminPageList($filter)
    {
        // return $this->adminPostList($filter, true);

        //默认使用前台排序规则
        $order    = $filter['sort_by'].' '.$filter['list_order'];
        $pageSize = empty($filter['page_size']) ? 10 : intval($filter['page_size']);

        if( empty($filter['sort_by']) ){
            $order = 'a.create_time desc';
        }

        $where = [
            'a.post_type'   => 2,
            'a.create_time' => ['>=', 0],
        ];

        //删除时间
        $where['a.delete_time'] = empty($filter['delete_time']) ? ['=',0] : $filter['delete_time'];
        // pre($where,1);
        
        $join = [
            ['__USER__ u', 'a.user_id = u.id']
        ];

        $field = 'a.*,u.user_login,u.user_nickname,u.user_email,u.user_login';

        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.published_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.published_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.published_time'] = ['<= time', $endTime];
            }
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.post_title'] = ['like', "%$keyword%"];
        }

        $repairPostModel = new RepairPostModel();
        $articles        = $repairPostModel->alias('a')->field($field)
                        -> join($join)
                        -> where($where)
                        -> order($order)
                        -> paginate($pageSize);

        // echo $repairPostModel->getLastSql();die;
        return $articles;
    }

    // 查询文章
    public function adminPostList($filter)
    {
        // 每页数据条数
        $pageSize = empty($filter['page_size']) ? 10 : intval($filter['page_size']);

        // 默认使用前台排序规则
        $order = $filter['sort_by'].' '.$filter['list_order'];
        if( empty($filter['sort_by']) ){
            $order = 'a.create_time desc';//SO;
        }

        if( empty($filter['category']) ){
            $where['a.type'] = ['eq', $filter['category']];
        }

        // 查询条件
        $where = [
            'a.post_type'   => 1,
            'a.create_time' => ['>=', 0],   //创建时间
            'a.delete_time' => 0            //删除时间
        ];

        // 站点ID
        $where['a.site_id'] = get_site_id();
        if( !empty($filter['site_id']) ){
            $where['a.site_id'] = $filter['site_id'];
        }

        /*// 审核状态
        if( !empty($filter['post_status']) ){
            if( $filter['post_status'] == 1 ){
                $where['a.post_status'] = [['>=',0], ['<',99]];     // 待审核(0--98)
            }else{
                $where['a.post_status'] = $filter['post_status'];   // 审核通过(99)或不通过(-1)
            }
        }*/

        // 连表
        // $join = [
        //     ['__USER__ u', 'a.user_id = u.id'],
        //     ['__REPAIR_CATEGORY_POST__ b', 'a.id = b.post_id']
        // ];

        //要查询的字段
        $field = 'a.*,u.user_login,u.user_nickname';

        //是否指定查询某个分类
        // $category = empty($filter['category']) ? 0 : intval($filter['category']);
        // if (!empty($category)) {
        //     $where['b.category_id'] = ['eq', $category];
        // }else{
        //     /*若不是超级管理员，只获取该角色拥有的操作分类下的文章*/
        //     if( session('ROLE_ID') != 1 ){
        //         /*获得有权限的分类id*/
        //         $idList                 = get_cat_priv('init', 'repair_category'); //获取该角色可查看的栏目分类id
        //         $where['b.category_id'] = ['in',$idList];
        //     }
        // }


        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.published_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.published_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.published_time'] = ['<= time', $endTime];
            }
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.post_title'] = ['like', "%$keyword%"];
        }

        $keyword = empty($filter['content']) ? '' : $filter['content'];
        if (!empty($content)) {
            $where['a.post_content'] = ['like', "%$content%"];
        }

        $repairPostModel = new RepairPostModel();
        $articles        = $repairPostModel->alias('a')->field($field)
                        //-> join($join)
                        -> where($where)
                        -> order($order)
                        -> paginate($pageSize);

        // echo $repairPostModel->getLastSql();die;
        return $articles;

    }

    public function publishedArticle($postId, $categoryId = 0)
    {
        $repairPostModel = new RepairPostModel();

        if (empty($categoryId)) {

            $where = [
                'post.post_type'      => 1,
                'post.published_time' => [['< time', time()], ['> time', 0]],
                'post.post_status'    => 99,
                'post.delete_time'    => 0,
                'post.id'             => $postId
            ];

            $article = $repairPostModel->alias('post')->field('post.*')
                ->where($where)
                ->find();
        } else {
            $where = [
                'post.post_type'       => 1,
                'post.published_time'  => [['< time', time()], ['> time', 0]],
                'post.post_status'     => 99,
                'post.delete_time'     => 0,
                'relation.category_id' => $categoryId,
                'relation.post_id'     => $postId
            ];

            $join    = [
                ['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']
            ];
            $article = $repairPostModel->alias('post')->field('post.*')
                ->join($join)
                ->where($where)
                ->find();
        }


        return $article;
    }

    //上一篇文章
    public function publishedPrevArticle($postId, $categoryId = 0)
    {
        $repairPostModel = new RepairPostModel();

        if (empty($categoryId)) {

            $where = [
                'post.post_type'      => 1,
                'post.published_time' => [['< time', time()], ['> time', 0]],
                'post.post_status'    => 99,
                'post.delete_time'    => 0,
                'post.id '            => ['<', $postId]
            ];

            $article = $repairPostModel->alias('post')->field('post.*')
                ->where($where)
                ->order('id', 'DESC')
                ->find();

        } else {
            $where = [
                'post.post_type'       => 1,
                'post.published_time'  => [['< time', time()], ['> time', 0]],
                'post.post_status'     => 99,
                'post.delete_time'     => 0,
                'relation.category_id' => $categoryId,
                'relation.post_id'     => ['<', $postId]
            ];

            $join    = [
                ['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']
            ];
            $article = $repairPostModel->alias('post')->field('post.*')
                ->join($join)
                ->where($where)
                ->order('id', 'DESC')
                ->find();
        }


        return $article;
    }

    //下一篇文章
    public function publishedNextArticle($postId, $categoryId = 0)
    {
        $repairPostModel = new RepairPostModel();

        if (empty($categoryId)) {

            $where = [
                'post.post_type'      => 1,
                'post.published_time' => [['< time', time()], ['> time', 0]],
                'post.post_status'    => 99,
                'post.delete_time'    => 0,
                'post.id'             => ['>', $postId]
            ];

            $article = $repairPostModel->alias('post')->field('post.*')
                ->where($where)
                ->order('id', 'ASC')
                ->find();
        } else {
            $where = [
                'post.post_type'       => 1,
                'post.published_time'  => [['< time', time()], ['> time', 0]],
                'post.post_status'     => 99,
                'post.delete_time'     => 0,
                'relation.category_id' => $categoryId,
                'relation.post_id'     => ['>', $postId]
            ];

            $join    = [
                ['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']
            ];
            $article = $repairPostModel->alias('post')->field('post.*')
                ->join($join)
                ->where($where)
                ->order('id', 'ASC')
                ->find();
        }


        return $article;
    }

    public function publishedPage($pageId)
    {

        $where = [
            'post_type'      => 2,
            'published_time' => [['< time', time()], ['> time', 0]],
            // 'post_status'    => 99,
            'delete_time'    => 0,
            'id'             => $pageId
        ];

        $repairPostModel = new RepairPostModel();
        $page            = $repairPostModel
            ->where($where)
            ->find();

        return $page;
    }

}
